package com.netsdk.lht2.push;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeCacheUtil {
    private final static Cache<String,TimeRecored> cache = CacheUtil.newFIFOCache(100);
    private final static Cache<String,TimeRecored> cache2 = CacheUtil.newFIFOCache(100);
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    private final static DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    public synchronized static TimeRecored get2(String utc){
        LocalDateTime utcTime = parseUtc(utc);
        TimeRecored utcTimeRecored = cache2.get(utc);
        if (utcTimeRecored == null){
            utcTimeRecored = new TimeRecored(0,"0");
            utcTimeRecored.startTime = utcTime;
            cache2.put(utc,utcTimeRecored);
        }
//        else {
//            if (utcTimeRecored.status == 1 || utcTimeRecored.status == 2){
//                LocalDateTime nextTime = utcTime.plusMinutes(1);
//                TimeRecored newUtcTimeRecored = new TimeRecored(0,"0");
//                newUtcTimeRecored.startTime = nextTime;
//                cache2.put(formateLocalDateTime(nextTime),newUtcTimeRecored);
//            }
//        }
        return utcTimeRecored;
    }

    public synchronized static TimeRecored get(String type,String utc){
        LocalDateTime utcTime = parseUtc(utc);
        TimeRecored utcTimeRecored = cache.get(type + utc);
        if (utcTimeRecored == null){
            LocalDateTime startTime = caculateMaxTime(type,utcTime);
            utcTimeRecored = new TimeRecored(0,type);
            utcTimeRecored.setStartTime(startTime.plusSeconds(1));
            utcTimeRecored.setEndTime(startTime.plusSeconds(1));
            cache.put(type + utc,utcTimeRecored);
        }

        return utcTimeRecored;
    }

    public static LocalDateTime caculateMaxTime(String type,LocalDateTime currentTime){
        LocalDateTime max = currentTime;
        for (TimeRecored timeRecored : cache) {
            if(type.equals(timeRecored.type)){
                if(timeRecored.getEndTime().isAfter(max)){
                    max = timeRecored.endTime;
                }
            }
        }
        return max;
    }


    public static LocalDateTime parseUtc(String utc ){
        return LocalDateTime.parse(utc,dateTimeFormatter);
    }

    public static String formateUtc(String utc ){
        return LocalDateTime.parse(utc,dateTimeFormatter).format(dateTimeFormatter2);
    }

    public static String formateLocalDateTime(LocalDateTime localDateTime ){
        return localDateTime.format(dateTimeFormatter);
    }


    static class TimeRecored {
        LocalDateTime startTime;
        LocalDateTime endTime;
        // 0创建，1执行中，2完成
        int status;
        String type;

        public TimeRecored(int status,String type) {
            this.status = status;
            this.type = type;
        }

        public LocalDateTime getStartTime() {
            return startTime;
        }

        public void setStartTime(LocalDateTime startTime) {
            this.startTime = startTime;
        }

        public LocalDateTime getEndTime() {
            return endTime;
        }

        public void setEndTime(LocalDateTime endTime) {
            this.endTime = endTime;
        }
    }
}
